<?php
session_start();
require_once("include/bdd.inc.php");
$Bnumero = $_SESSION['Bnumero'];
if(isset($_POST['nouvelleLigne'])) {
	$motif = $_POST['motif'];
	$villeDepart = $_POST['villeDepart'];
	$villeArrivee = $_POST['villeArrivee'];
	$trajet = 'Déplacement de '.$villeDepart.' à '.$villeArrivee;
	$nbKm = $_POST['nbKilometres'];
	$coutPeage = $_POST['coutPeage'];
	$coutRepas = $_POST['coutRepas'];
	$coutHebergement = $_POST['coutHebergement'];
	$date = date("d/m/Y");
	
	$req="INSERT INTO lignesfrais(FRnumeroBordereau, FRnumeroLigne, FRdateLigneFrais, FRcodeMotif, FRtrajet, FRkm, FRcoutPeage, FRcoutRepas, FRcoutHebergement) 
	VALUES($Bnumero, 1, '$date', '$motif', '$trajet', $nbKm, $coutPeage, $coutRepas, $coutHebergement)";
	$resultat=mysql_query($req);
	header('Location: bordereaux.php');
}

if(isset($_POST['action'])){
	if($_POST['action'] == "Supprimer"){
		if($_POST['choix']){
			foreach($_POST['choix'] as $numeroLigne){
				$req="DELETE FROM lignesfrais 
				WHERE FRnumeroBordereau = $Bnumero
				AND FRnumeroLigne = $numeroLigne";
				$resultat=mysql_query($req);
			}
		}
	}
	header('Location: bordereaux.php');
}

if(isset($_POST['modif-valider'])){
	$Lnumero=$_POST['Lnumero'];
	$motif = $_POST['motif'];
	$villeDepart = $_POST['villeDepart'];
	$villeArrivee = $_POST['villeArrivee'];
	$trajet = 'Déplacement de '.$villeDepart.' à '.$villeArrivee;
	$nbKm = $_POST['nbKm'];
	$coutPeage = $_POST['peage'];
	$coutRepas = $_POST['repas'];
	$coutHebergement = $_POST['hebergement'];
	
	$req="UPDATE lignesfrais SET
	FRcodeMotif='$motif',
	FRtrajet='$trajet',
	FRkm=$nbKm,
	FRcoutPeage=$coutPeage,
	FRcoutRepas=$coutRepas,
	FRcoutHebergement=$coutHebergement
	WHERE FRnumeroBordereau=$Bnumero
	AND FRnumeroLigne=$Lnumero";
	$resultat=mysql_query($req);
	
	header('Location: bordereaux.php');
}

if(isset($_POST['annuler'])){
	header('Location: bordereaux.php');
}
?>